/**
 * 
 */
package com.haizhi.user.vo.factory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.haizhi.bean.TbProduct;
import com.haizhi.bean.TbProductData;
import com.haizhi.service.ProductDataService;
import com.haizhi.service.ProductService;
import com.haizhi.user.vo.ProductVo;
import com.haizhi.util.SpringUtil;

/**
 * @author jiangjintai
 *
 */
public class ProductVoFactory extends BaseFactory<TbProduct,ProductVo> {
	static Logger logger = Logger.getLogger(ProductVoFactory.class);
	@Autowired
	ProductDataService productDataService=(ProductDataService) SpringUtil.getBean("productDataService");
	/**
	 * @param t
	 * @param clazz
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public ProductVoFactory(TbProduct t)
			throws InstantiationException, IllegalAccessException {
		super(t, ProductVo.class);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.user.vo.factory.BaseFactory#doOrderThingForVo(java.lang.Object)
	 */
	@Override
	protected void doOrderThingForVo(ProductVo productVo) {
		productVo.setUserId(this.getTb().getTbUser().getUserId());
		productVo.setUserName(this.getTb().getTbUser().getUserName());
		productVo.setUserSex(this.getTb().getTbUser().getUserSex());
		//封装当前数据版本
		logger.info(productDataService);
		List<TbProductData> tbProductDataList = productDataService.findByMaxVersion(this.getTb());
		List<Integer> jsonList= new ArrayList<Integer>();
		List<String> fileList = new ArrayList<String>();
		for(TbProductData tpd:tbProductDataList){
			if(tpd.getDataType()==1){
				if(!tpd.getDataContent().trim().isEmpty()){
					//代表json
					jsonList.add(tpd.getDataId());
				}
				
			}else{
				fileList.add(tpd.getDataContent());
			}
		}		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("json", jsonList);
		map.put("file", fileList);
		productVo.setCurrentData(map);
		if(this.getTb().getTbProduct()!=null){
			productVo.setParentProductId(this.getTb().getTbProduct().getProductId());
			productVo.setParentProductImgUrl(this.getTb().getTbProduct().getProductImgUrl());
			productVo.setParentProductTitle(this.getTb().getTbProduct().getProductTitle());
		}
	}

}
